977F - Consecutive Subsequence - CodeForces Solution


dp *1700

Please click on ads to support us..

Python Code:

from collections import *
from math import inf
import math, os, sys, heapq, bisect, random
from functools import lru_cache
from itertools import *
from io import BytesIO, IOBase
def inp(): return sys.stdin.readline().rstrip("\r\n")
def out(var): sys.stdout.write(str(var))  def inpu(): return int(inp())
def lis(): return list(map(int, inp().split()))
def stringlis(): return list(map(str, inp().split()))
def sep(): return map(int, inp().split())
def strsep(): return map(str, inp().split())
def fsep(): return map(float, inp().split())
M,M1=1000000007,998244353
BUFSIZE = 8192
def main():
    how_much_noob_I_am = 1
        for __ in range(how_much_noob_I_am):
        n=inpu()
        arr = lis()
        ind = defaultdict(list)
        for i in range(n):
            ind[arr[i]].append(i+1)
        d=defaultdict(int)
        for i in range(n):
            d[arr[i]] = d[arr[i]-1]+1
        max1 = -inf
        res=-1
        for i in d:
            if d[i]>max1:
                res=i
                max1 = d[i]
        start = res-max1+1
        ans=[]
        req=-1
        for i in range(start,start+max1):
            req = bisect.bisect_left(ind[i],req)
            req = ind[i][req]
            ans.append(req)
        print(len(ans))
        print(*ans)
if __name__ == '__main__':
    main()

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
	int n;
	cin>>n;
	vector<ll> v(n+1);
	for(int i=1;i<=n;i++){
		cin>>v[i];
	}
	map<ll,pair<ll,ll>> m;
	ll parent[n+1]={0};
	for(ll i=1;i<=n;i++){
		if(m.count(v[i]-1)){
			ll temp=(m[v[i]-1].first)+1;
			m[v[i]]={temp,i};
			parent[i]=m[v[i]-1].second;
		}
		else{
			if(!m.count(v[i]))
			m[v[i]]={1,i};
		}
	}
	ll ans=1;
	ll i=0;
	for(auto it:m){
		if(ans<it.second.first){
			ans=it.second.first;
			i=it.second.second;
		}
	}
	cout<<ans<<endl;
	if(ans==1){
		cout<<1;
		return 0;
	}
	stack<ll> s;
	while(i!=0){
		s.push(i);
		i=parent[i];
	}
	while(!s.empty()){
		ll temp=s.top();
		cout<<temp<<" ";
		s.pop();
	}
	return 0;
}


Comments

Submit
0 Comments
More Questions

442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal